;Make all position reference become relative
AutoItSetOption("MouseCoordMode", 0)

Global $buyPrice = 0
Global $sellPrice = 0
Global $midPrice = 0

$buyTitle = "4792858: OANDA - MetaTrader - Demo Account"
$sellTitle = "5204653: OANDA - MetaTrader - Demo Account"

clickNewOrder($buyTitle)
clickNewOrder($sellTitle)

;Click Sell
MouseClick("", 444, 261)

Send("!{TAB}")

;Click Buy
MouseClick("", 620, 262)

WinActivate("Order")
waitUntilOkVisible()
Sleep(500)
Send("!{TAB}")
If WinExists("Order") Then
   WinActivate("Order")
   Sleep(500)
   waitUntilOkVisible()
EndIf

Sleep(3000)

getBuyPrice($buyTitle)
getSellPrice($sellTitle)

$midPrice = ($buyPrice + $sellPrice) / 2

setBuySide($buyTitle)
Send("!{TAB}")
setSellSide($sellTitle)

Func getBuyPrice($title)
   
   WinActivate($title)
   ;ControlFocus( $title, "", "[CLASS:SysListView32; INSTANCE:5]")
   ;ControlListView ( $title, "", "[CLASS:SysListView32; INSTANCE:5]", "Select" , 0 )
   ;Send("{ENTER}")
   MouseClick("right", 869, 953)
   MouseClick("", 958, 763)
   WinWaitActive("Order #")

   Local $titlex = WinGetTitle("Order #")
   Local $titleArr = StringSplit($titlex, " ")
   $buyPrice = Number($titleArr[7])
   
   Send("{ESC}")
   
EndFunc

Func getSellPrice($title)
   
   WinActivate($title)
   ;ControlFocus( $title, "", "[CLASS:SysListView32; INSTANCE:5]")
   ;ControlListView ( $title, "", "[CLASS:SysListView32; INSTANCE:5]", "Select" , 0 )
   ;Send("{ENTER}")
   MouseClick("right", 869, 953)
   MouseClick("", 958, 763)
   WinWaitActive("Order #")

   Local $titlex = WinGetTitle("Order #")
   Local $titleArr = StringSplit($titlex, " ")
   $sellPrice = Number($titleArr[7])
   
   Send("{ESC}")
   
EndFunc

Func setBuySide($title)

   WinActivate($title)
   ;ControlFocus( $title, "", "[CLASS:SysListView32; INSTANCE:5]")
   ;ControlListView ( $title, "", "[CLASS:SysListView32; INSTANCE:5]", "Select" , 0 )
   ;Send("{ENTER}")
   MouseClick("right", 869, 953)
   MouseClick("", 958, 763)
   WinWaitActive("Order #")

   Local $takeProfit = Round($buyPrice * 1.004, 3)
   Local $stopLoss = Round($buyPrice * 0.9966, 3)

   ;select stop loss
   MouseClick("", 436, 264)
   Send($stopLoss)

   ;select take profit
   MouseClick("", 625, 263)
   Send($takeProfit)

   ;click modify
   MouseClick("", 534, 295)

   waitUntilOkVisible()
   
   Sleep(1000)
   
EndFunc

Func setSellSide($title)

   WinActivate($title)
   ;ControlFocus( $title, "", "[CLASS:SysListView32; INSTANCE:5]")
   ;ControlListView ( $title, "", "[CLASS:SysListView32; INSTANCE:5]", "Select" , 0 )
   ;Send("{ENTER}")
   MouseClick("right", 869, 953)
   MouseClick("", 958, 763)
   WinWaitActive("Order #")

   Local $takeProfit = Round($sellPrice * 0.996, 3)
   Local $stopLoss = Round($sellPrice * 1.0034, 3)

   ;select stop loss
   MouseClick("", 436, 264)
   Send($stopLoss)

   ;select take profit
   MouseClick("", 625, 263)
   Send($takeProfit)

   ;click modify
   MouseClick("", 534, 295)

   waitUntilOkVisible()
   Sleep(1000)
   
EndFunc

Func clickNewOrder($title)

   WinActivate($title)
   WinWaitActive($title)

   ;Click New Order
   ControlCommand($title, "", "[CLASS:ToolbarWindow32; INSTANCE:4]", "SendCommandID", 33266)
   ;ControlClick($title, "", "[ID:99]")
   
EndFunc

Func waitUntilOkVisible()
   
   While 1
	  ConsoleWrite("loop" & @CRLF)
	  If WinExists("Order") Then
		 WinActivate("Order")
		 $x = ControlCommand("", "", "[CLASS:Button; INSTANCE:22]", "IsVisible", "")
		 If $x = 1 Then
			ControlClick("Order", "", "[CLASS:Button; INSTANCE:22]")
			ExitLoop
		 EndIf
	  Else
		 ExitLoop
	  EndIf
	  
	  Sleep(250)
   WEnd
   
   ;MouseClick("", 428, 300)
   
EndFunc
